package cn.iocoder.yudao.module.resource.controller.admin.directorybaseinfo;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.resource.controller.admin.directorybaseinfo.vo.*;
import cn.iocoder.yudao.module.resource.dal.dataobject.directorybaseinfo.DirectoryBaseInfoDO;
import cn.iocoder.yudao.module.resource.service.directorybaseinfo.DirectoryBaseInfoService;

@Tag(name = "管理后台 - 接口基本信息")
@RestController
@RequestMapping("/resource/directory-base-info")
@Validated
public class DirectoryBaseInfoController {

    @Resource
    private DirectoryBaseInfoService directoryBaseInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建接口基本信息")
    @PreAuthorize("@ss.hasPermission('resource:directory-base-info:create')")
    public CommonResult<String> createDirectoryBaseInfo(@Valid @RequestBody DirectoryBaseInfoSaveReqVO createReqVO) {
        return success(directoryBaseInfoService.createDirectoryBaseInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新接口基本信息")
    @PreAuthorize("@ss.hasPermission('resource:directory-base-info:update')")
    public CommonResult<Boolean> updateDirectoryBaseInfo(@Valid @RequestBody DirectoryBaseInfoSaveReqVO updateReqVO) {
        directoryBaseInfoService.updateDirectoryBaseInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除接口基本信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('resource:directory-base-info:delete')")
    public CommonResult<Boolean> deleteDirectoryBaseInfo(@RequestParam("id") String id) {
        directoryBaseInfoService.deleteDirectoryBaseInfo(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除接口基本信息")
                @PreAuthorize("@ss.hasPermission('resource:directory-base-info:delete')")
    public CommonResult<Boolean> deleteDirectoryBaseInfoList(@RequestParam("ids") List<String> ids) {
        directoryBaseInfoService.deleteDirectoryBaseInfoListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得接口基本信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('resource:directory-base-info:query')")
    public CommonResult<DirectoryBaseInfoRespVO> getDirectoryBaseInfo(@RequestParam("id") String id) {
        DirectoryBaseInfoDO directoryBaseInfo = directoryBaseInfoService.getDirectoryBaseInfo(id);
        return success(BeanUtils.toBean(directoryBaseInfo, DirectoryBaseInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得接口基本信息分页")
    @PreAuthorize("@ss.hasPermission('resource:directory-base-info:query')")
    public CommonResult<PageResult<DirectoryBaseInfoRespVO>> getDirectoryBaseInfoPage(@Valid DirectoryBaseInfoPageReqVO pageReqVO) {
        PageResult<DirectoryBaseInfoDO> pageResult = directoryBaseInfoService.getDirectoryBaseInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DirectoryBaseInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出接口基本信息 Excel")
    @PreAuthorize("@ss.hasPermission('resource:directory-base-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDirectoryBaseInfoExcel(@Valid DirectoryBaseInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DirectoryBaseInfoDO> list = directoryBaseInfoService.getDirectoryBaseInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "接口基本信息.xls", "数据", DirectoryBaseInfoRespVO.class,
                        BeanUtils.toBean(list, DirectoryBaseInfoRespVO.class));
    }

}